<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>数据传输加解密示例</title>
	</head>
	<script type="text/javascript" src = "../static/js/aes.js"></script>
	<script type="text/javascript" src = "../static/js/crypto-js.js"></script>
	<script type="text/javascript" src = "../static/js/pad-zeropadding.js"></script>
	<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<body>
	<input type="button" value="获取数据" onclick="getData()"/>
	<input type="button" value="发送数据" onclick="sendData()"/>
	<input type="button" value="test" onclick="testData()"/>
	<script>
	function getData() {
		$.ajax({
	        type: "GET",
	        url:"/user/findById",
	        data:{"id":2},
	        dataType:'text',
	        success: function(resData) {
	        	alert(resData);
// 	        	alert(Decrypt2(resData));
	        	alert(Decrypt2("1C66164E6D1085B241BF2E00DD01A0395F977C825299E89077750C4F57C55B0F0BF60100D067C8203ECEFA8E99CF00AD9A212D3F00D4B9FBF6F86D60607460ED0E17F74D318EC8775D20D98DB96E4390F2543D65DB48FE8CF4CF3F253FAA3D5348A25BC3ECC1072150D650CE1D2C10B871E08D0F14FDBC975B1F1BC17D668A10FD7854EEC8FDA01D6ED3FF2BE63139C569E7977ACAFA7317252BD65A693DC874D32BAB42F1C155A0B83A7AFF673CA4DD91333498E6F387FCE5A56DE721F44A089065CB03C63F3C85658E0839FF321177"));
	        }
		});
	}
	
	function sendData() {
		alert("发送的数据："+Encrypt(JSON.stringify({id:1,name:"尹吉欢"})));
		$.ajax({
	        type: "POST",
	        url:"/save",
	        data:Encrypt(JSON.stringify({id:1,name:"尹吉欢"})),
	        dataType:'text',
	        contentType: "application/json",
	        success: function(resData) {
	        	alert("返回的数据："+resData);
	        	alert("解密之后："+Decrypt(resData));
	        	alert("解密之后转json对象："+JSON.parse(Decrypt(resData)).name);
	        }
		}); 
	}
	
	function testData() {
		var a = Encrypt('{"id":1,"nickname":"yu","profilePhoto":"","phone":"","password":"","openid":"1","status":0,"balance":0,"updateTime":1555050051000,"createTime":1555050051000}');
		console.log(a);
		var c = 'W+QyyX58oOITKR/fUVUqM735Khs7bLhpz+srIUSPPmytjvbMMfO49yWIypdCNbtpcdZAsWQ9at6A6SCL+p1fcZM8k23BidL9RMYddGBCioUhuZgE2MJaZU5LJtrKAWaFxE7pla6U15wlLcDpoXPiko2QFGDLp5bIZbV+pIOmfwE39KNFAxvxuFNQRmd8m6eF/HOK69ZPO97MMuby7pTOuQ==';
		var b = Decrypt(c);
		alert(b)
	}
	
	var key = CryptoJS.enc.Utf8.parse("12345678");
	function Encrypt(word) {
		var srcs = CryptoJS.enc.Utf8.parse(word);
		var encrypted = CryptoJS.AES.encrypt(srcs, key, {
			mode : CryptoJS.mode.ECB,
			padding : CryptoJS.pad.Pkcs7
		});
		return encrypted.toString();
	}
	
	function Decrypt(word) {
		var decrypt = CryptoJS.AES.decrypt(word, key, {
			mode : CryptoJS.mode.ECB,
			padding : CryptoJS.pad.Pkcs7
		});
		return CryptoJS.enc.Utf8.stringify(decrypt).toString();
	}
	
	function Decrypt2(word) {
// 		var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
// 	    var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
	 var a = CryptoJS.AES.decrypt(word,"12345678").toString(CryptoJS.enc.Utf8);
	 console.log(a)
// 		var encryptedBase64Str = word;
// 	    var options = {
// 	      mode: CryptoJS.mode.ECB,
// 	      padding: CryptoJS.pad.Pkcs7
// 	    };
// 	    var key1 = CryptoJS.enc.Utf8.parse("12345678");
// 	    // 解密
// 	    var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key1, options);
// 	    // 解密后，需要按照Utf8的方式将明文转位字符串
// 	    var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
// 	    return decryptedStr ;
	}
	</script>  
</body>

</html>